<?php
// based on wp-admin/edit-form-ajax-cat.php

require_once('../../../wp-config.php');
require_once('../../../wp-admin/admin-functions.php');
require_once('../../../wp-admin/admin-db.php');
require_once('EventManager.php');
require_once(ABSPATH . "wp-content/plugins/EventManager/includes/export-functions.php");

get_currentuserinfo();

$event_id = htmlentities($_REQUEST['event_id']);
$manager_id = $wpdb->get_var("select manager_id from $em_event_table where id = $event_id");

if (em_can_admin_events() || $manager_id != $current_user->id) {

	function get_out_now() { exit; }
	add_action('shutdown', 'get_out_now', -1);

	//Implemented this and posting from AJAX to prevent IE from caching responses
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // HTTP/1.1
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	header("Content-type: application/vnd.ms-excel");//set content type
	header('Content-Disposition: attachment; filename="event-participants.xls"');
	header('Pragma: public');

	$st = "<td class=xl2216681 nowrap>";
	$et = "</td>";
	$s = $et . $st;

	$basic_header = array('Confirmation', 'Email', 'First Name', 'Last Name', 'Address 1', 'Address 2',
				'City', 'State', 'Zip', 'Phone');


	switch ($_REQUEST['action']) {
		case "simple":
			$participants = $wpdb->get_results("SELECT * from $em_registration_table where event_id = '$event_id'");
			em_excelHeader();

			//echo header
			echo '<tr>' . $st . implode($s, $basic_header) . $et . "</tr>\r\n";

			//echo data
			if ($participants) {
				foreach ($participants as $participant) {
					echo '<tr>';
					echo $st . $participant->event_id
					. "-" . $participant->id
					. $s . $participant->email_address
					. $s . $participant->first_name
					. $s . $participant->last_name
					. $s . $participant->address1
					. $s . $participant->address2
					. $s . $participant->city
					. $s . $participant->state
					. $s . $participant->zip
					. $s . $participant->phone . $et;
					echo "</tr>\r\n";
				}
			} else {
				echo "<tr><td>No participant data has been collected.</td></tr>";
			}
			em_excelFooter();
			break;
		case "complete":
			//query for custom questions
			$question_sequence = array();
			$questions = $wpdb->get_results("select question, sequence from $em_question_table where event_id = '$event_id' order by sequence");
			foreach ($questions as $question) {
				array_push($basic_header, $question->question);
				array_push($question_sequence, $question->sequence);
			}

			$participants = $wpdb->get_results("SELECT * from $em_registration_table where event_id = '$event_id'");
			em_excelHeader();

			//echo header
			echo '<tr>' . $st . implode($s, $basic_header) . $et . "</tr>\r\n";

			//echo data
			if ($participants) {
				foreach ($participants as $participant) {
					echo '<tr>';
					echo $st . $participant->event_id
					. "-" . $participant->id
					. $s . $participant->email_address
					. $s . $participant->first_name
					. $s . $participant->last_name
					. $s . $participant->address1
					. $s . $participant->address2
					. $s . $participant->city
					. $s . $participant->state
					. $s . $participant->zip
					. $s . $participant->phone;
	
					$answers = $wpdb->get_results("select a.answer from $em_answer_table a join $em_question_table q on " .
							"q.id = a.question_id where registration_id = '$participant->id' order by q.sequence");
	
					foreach($answers as $answer) {
						echo $s . $answer->answer;
					}
	
					echo $et . "</tr>\r\n";
				}
			} else {
				echo "<tr><td>No participant data has been collected.</td></tr>";
			}
			em_excelFooter();
			break;
		default:
			header("HTTP/1.0 404 Not Found");
	}

} else {
	echo "It looks like you can't download participant data.  Make sure you allowed yourself to administer events under Options>EventManagement.";
}
